var express = require('express');
var router = express.Router();
var path = require('path');
var async  = require( 'async'); // 同步控制
var redis = require("redis");
var Scripto = require('redis-scripto');

var CONFIG = require('../../../../../common/config');
var CODE = require('../../../../../common/code');




var yjxRedisManager = (function () {
    function yjxRedisManager() {
    }

    /**
     * 初始化 redis 的脚本管理器
     *
     * @author 王俊
     * @date 2017-11-24
     */
    yjxRedisManager.setupRedisClients = function () {
        // 如果初始化过就不再初始化
        if(yjxRedisManager.scriptManagers) {
            return;
        }
        //----------------- 搜索相关 begin-----------------------
        yjxRedisManager.scriptManagers = {};
        var searchLuaPath = path.resolve(__dirname, '../lua/search');
        var config = redis.createClient({
            host:CONFIG.YJX_JAVA_SEARCH_REDIS_CONFIG_DOMAIN,
            auth_pass:CONFIG.YJX_JAVA_SEARCH_REDIS_CONFIG_PASSWORD,
            port:CONFIG.YJX_JAVA_SEARCH_REDIS_CONFIG_PORT,
            db:CONFIG.YJX_JAVA_SEARCH_REDIS_CONFIG_DB_NUMBER,
            ttl:CONFIG.YJX_JAVA_SEARCH_REDIS_CONFIG_TTL
        });
        var mgrConfig = new Scripto(config);
        mgrConfig.loadFromDir(searchLuaPath);
        var client2 = redis.createClient({
            host:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_DOMAIN,
            auth_pass:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_PASSWORD,
            port:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_PORT,
            db:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_DB_NUMBER,
            ttl:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_TTL
        });
        var mgrCache2 = new Scripto(client2);
        mgrCache2.loadFromDir(searchLuaPath);

        var client1 = redis.createClient({
            host:CONFIG.YJX_SEARCH_REDIS_CACHE1_DOMAIN,
            auth_pass:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE1_PASSWORD,
            port:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE1_PORT,
            db:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE1_DB_NUMBER,
            ttl:CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE1_TTL
        });
        var mgrCache1 = new Scripto(client1);
        mgrCache1.loadFromDir(searchLuaPath);
        // 记录配置和两个缓存 manager
        yjxRedisManager.scriptManagers.config = mgrConfig;
        yjxRedisManager.scriptManagers.cache1 = mgrCache1;
        yjxRedisManager.scriptManagers.cache2 = mgrCache2;

        //----------------- 搜索相关 end-----------------------
    };


    /**
     * 获取在线的搜索 redis db 的脚本管理器
     *
     * @author 王俊
     * @date 2017-11-24
     *
     * @param callback
     *          error: 获取线上搜索db 的脚本管理器失败
     *          scriptManager: 线上的搜索脚本管理器
     */
    yjxRedisManager.getOnlineSearchRedisDBMgr = function (callback) {

        yjxRedisManager.scriptManagers.config.run('yjx_search_get_online_db', [], [], function (error, result) {
            if(error) {
                return callback(error, null);
            }
            // redis configDb 中记录编号;
            var onlineDBFlg = Number(result);
            if (onlineDBFlg === CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE1_DB_NUMBER) {
                return callback(null, yjxRedisManager.scriptManagers.cache1);
            } else if(onlineDBFlg === CONFIG.YJX_JAVA_SEARCH_REDIS_CACHE2_DB_NUMBER) {
                return callback(null, yjxRedisManager.scriptManagers.cache2);
            } else {
                return callback('yjx redis search config db 数据异常', null);
            }
        });
    };


    //------------------------------------------------------------------
    return yjxRedisManager;
})();

// 初始化 redis clients
yjxRedisManager.setupRedisClients();




//------------------------------------------------------------------
module.exports = yjxRedisManager;

